#include "gamewindowlabel.h"

GameWindowLabel::GameWindowLabel(QWidget *parent) : QLabel(parent)
{
    level = 1;
    score = 0;

    cannonLen = 260;
    lockedWord = NULL;


    setStyleSheet("background-color:#5095Bb");


    timerCannon = new QTimer(this);
    timerCannon->setSingleShot(true);
    connect(timerCannon, &QTimer::timeout, this, [=](){
        cannonLen = 260;
        update();
    });

    timerRunning = new QTimer(this);
    connect(timerRunning, &QTimer::timeout, this, [=](){

        for(int i=0; i<wordList.size(); i++) {
            EnemyWord *word = wordList[i];
            word->setPos(QPoint(word->getPos().x(), word->getPos().y() + 1));
            word->ballsApproach();
        }

        for(int i=0; i<wordList.size(); i++) {
            EnemyWord *word = wordList[i];
            if(word->getPos().y() > gameDeadLine) {
                if(lockedWord == word) {
                    lockedWord = NULL;
                }
                wordList.removeAt(i);
                delete word;
            }
        }



        update();
    });

    timerRunning->start(20);

}

void GameWindowLabel::paintEvent(QPaintEvent *e) {
    Q_UNUSED(e);
    QPainter painter(this);

    QRect rect = this->rect();

    QRect scoreRect;
    QRect levelRect;

    levelRect.setTop(rect.bottom() - 90);
    levelRect.setBottom(rect.bottom() - 20);
    levelRect.setLeft(20);
    levelRect.setRight(20 + rect.width() / 4);

    scoreRect.setTop(rect.bottom() - 90);
    scoreRect.setBottom(rect.bottom() - 20);
    scoreRect.setLeft(rect.right() - 20 - rect.width() / 4);
    scoreRect.setRight(rect.right() - 20);

    QRect fireRect;

    fireRect.setTop(rect.bottom() - 100);
    fireRect.setBottom(rect.bottom() + 220);
    fireRect.setLeft(rect.width() / 2 - 160);
    fireRect.setRight(rect.width() / 2 + 160);
    firePoint = fireRect.center();

    //painter.drawRect(fireRect);

    QPen pen = painter.pen();
    pen.setWidth(3);
    painter.setPen(pen);

    gameDeadLine = fireRect.top() - 20;
    painter.setBrush(Qt::gray);
    //painter.drawRect(0, gameDeadLine, rect.width(), 500);
    //painter.drawLine(0, gameDeadLine, rect.right(), gameDeadLine);

    pen.setWidth(5);
    painter.setPen(pen);

    painter.drawRect(levelRect);
    painter.drawRect(scoreRect);

    //painter.drawLine(firePoint, targetPoint);
    QFont font = painter.font();
    font.setFamily("Courier New");
    font.setPixelSize(40);
    font.setBold(true);
    painter.setFont(font);

    for(int i=0; i<wordList.size(); i++) {
        EnemyWord *word = wordList[i];
        QRect wordRect;
        wordRect.setTop(word->getPos().y() - word->getHalfH());
        wordRect.setBottom(word->getPos().y() + word->getHalfH());
        wordRect.setLeft(word->getPos().x() - word->getHalfW());
        wordRect.setRight(word->getPos().x() + word->getHalfW());
        painter.drawRect(wordRect);
        QString wordStr = word->getWord();
        painter.drawText(wordRect, Qt::AlignCenter, wordStr);
        if(word == lockedWord) {
            int curIndex = word->getCurIndex();
            if(curIndex) {
                //pen.setColor(Qt::red);
                painter.setPen(Qt::red);
                //painter.setPen(pen);
                QString s = wordStr.mid(0, curIndex);
                for(int j=curIndex; j<wordStr.size(); j++) {
                    s += " ";
                }
                painter.drawText(wordRect, Qt::AlignCenter, s);
                //pen.setColor(Qt::black);
                painter.setPen(pen);
            }
        }


        QList<CannonBall *> *ballList = word->getBallList();
        for(int j=0; j<ballList->size(); j++) {
            QRect r;
            r.setTop((*ballList)[j]->getCurPos().y() - 15);
            r.setBottom((*ballList)[j]->getCurPos().y() + 15);
            r.setLeft((*ballList)[j]->getCurPos().x() - 15);
            r.setRight((*ballList)[j]->getCurPos().x() + 15);
            painter.drawEllipse(r);
        }

    }

    double angle = atan(1.0 * (targetPoint.x() - firePoint.x()) / (firePoint.y() - targetPoint.y()));

    painter.save();
    painter.translate(firePoint);
    painter.rotate(angle * 180 / 3.1415926);
    QRect rectCannon;
    rectCannon.setTop(0 - cannonLen);
    rectCannon.setBottom(0 + 220);
    rectCannon.setLeft(0 - 20);
    rectCannon.setRight(0 + 20);
    painter.drawRect(rectCannon);
    painter.restore();

    //painter.setBrush(Qt::black);
    painter.drawEllipse(fireRect);
}

void GameWindowLabel::mouseMoveEvent(QMouseEvent *ev) {
        //qDebug() << ev->x() << ev->y();
        targetPoint.setX(ev->x());
        targetPoint.setY(ev->y());

//        for(int i=0; i<wordList.size(); i++) {
//            EnemyWord *word = wordList[i];
//            word->setPos(QPoint(ev->x(), ev->y()));
//        }

        update();
}


void GameWindowLabel::getKey(const QString &c) {
    if(c == "")
        return;
    cannonLen = 240;
    update();
    timerCannon->start(200);

    if(lockedWord == NULL) {
        // 找一个最佳的单词并锁定
        int h_min = 0;
        EnemyWord *lword = NULL;
        for(int i=0; i<wordList.size(); i++) {
            EnemyWord *word = wordList[i];
            int y = word->getPos().y();
            if(word->getWord()[0] == c && y > h_min) {
                h_min = y;
                lword = word;
            }
        }
        lockedWord = lword;
    }
    if(lockedWord) {
        targetPoint = lockedWord->getPos();
        if(false == lockedWord->appendBall(c)) {
            lockedWord = NULL;
        }
    }



    qDebug() << c;
}

void GameWindowLabel::generateWords(int cnt) {
    srand(QTime::currentTime().msec());
    for(int i=0; i<cnt; i++) {
        int w = rand() % width();
        int h = rand() % height() - height();
        EnemyWord *word = new EnemyWord("Hello", firePoint, QPoint(w, h));
        if(word->getPos().x() - word->getHalfW() < 0 ||
                word->getPos().x() + word->getHalfW() > width()) {
            delete word;
            i--;
        } else {
            wordList.append(word);
        }
    }
}
